import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../constants/colors.dart';
import '../controller/DatabaseListController.dart';
import '../widget/custom_avatar.dart';

class HomeNormalPage extends StatefulWidget {
  final Function seeAll;

  const HomeNormalPage({super.key, required this.seeAll});

  @override
  State<HomeNormalPage> createState() => _HomeNormalPageState();
}

class _HomeNormalPageState extends State<HomeNormalPage> {
  @override
  Widget build(BuildContext context) {
    final DatabaseController databaseController =
        Get.find<DatabaseController>();
    // databaseController.fetchMineData();

    return Container(
      decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage('images/img_home_bg.png'), fit: BoxFit.fill)),
      child: Container(
        padding: const EdgeInsets.only(left: 24, right: 24),
        child: Container(
          width: 237,
          margin: const EdgeInsets.only(top: 290),
          child: Stack(
            children: [
              Image.asset('images/img_transparent_bg.png'),
              Positioned(
                  top: 0,
                  right: 0,
                  child: GestureDetector(
                    onTap: () {
                      widget.seeAll();
                    },
                    child: Image.asset("images/icon_see_all.png",
                        width: 70, height: 40),
                  )),
              Padding(
                padding: const EdgeInsets.only(top: 24, left: 10, right: 10),
                child: Obx(() {
                  return
                    databaseController.homeCreation.value.userId == 0? const SizedBox():
                      Column(
                    children: [
                      Row(
                        children: [
                          CustomAvatar(
                              width: 42,
                              height: 42,
                              netPath:
                                  databaseController.homeAuthor.value.avatar ?? ''),
                          const SizedBox(width: 12),
                          SizedBox(width: 160,child: Text(
                            databaseController.homeAuthor.value.nickname ?? '',
                            style: const TextStyle(
                                fontSize: 24,
                                fontWeight: FontWeight.w900,
                                color: AppColors.describeColor),
                            maxLines: 1,
                            overflow: TextOverflow.visible,
                          ),)
                        ],
                      ),
                      const SizedBox(height: 18),
                      SizedBox(
                        height: 220,
                        width: double.infinity,
                        child: creationItem(
                            ClipRRect(
                              borderRadius: BorderRadius.circular(14),
                              child: Image.asset(
                                  databaseController.homeCreation.value.cover ?? '',
                                  fit: BoxFit.cover),
                            )),
                      ),
                      const SizedBox(
                        height: 12,
                      ),
                      Row(
                        children: [
                          Expanded(child: GestureDetector(child: Text(
                            databaseController.homeCreation.value.title ?? '',
                            style: const TextStyle(
                                fontWeight: FontWeight.w900,
                                color: Colors.white,
                                fontSize: 18),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            textAlign: TextAlign.left,
                          ),onTap: (){widget.seeAll();},)),
                          GestureDetector(child: Image.asset(databaseController.
                          homeCreation.value.like == 1?"images/icon_liked.png":"images/icon_unlike.png",width: 24,height: 24,),onTap: (){
                            databaseController.updateCreationFavoriteByPostId(databaseController.homeCreation.value.postId!,
                                databaseController.homeCreation.value.like == 1?false:true);
                            setState(() {});
                          },)
                        ],
                      )
                    ],
                  );
                }),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget creationItem(Widget childview) {
    return InkWell(
        onTap: () {
          widget.seeAll();
        },
        child: childview);
  }
}


